export const request = (params) => {

  // 判断 url 中请求的私有路径，带上求情头
  let header = {
    ...params.header
  };
  header["token"] = wx.getStorageSync("token");
  if (!params.loading) {
    // console.log(params);
    // wx.showLoading({
    //   title: '数据加载中',
    //   mask: true
    // });
  }
  // 定义公共的url
  const baseUrl = "https://water.zhongaming.com"
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header: header,
      url: baseUrl + params.url,
      success: (result) => {
        if (result.data.code === 201) {
          wx.showToast({
            title: result.data.msg,
            icon:'none',
            duration:2000
          })
          return resolve(false);
        }
        if (result.data.code === 202) {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '/pages/login/login?msg=' + result.data.msg,
          })
          return resolve(false);
        }
        if (result.data.code === 204) {
          wx.showToast({
            title: result.data.msg,
            icon: 'none'
          })
          return resolve(false);
        }
        resolve(result);
      },
      fail: (err) => {
        reject(err);
      },
      complete: () => {
        if (!params.loading) {
          // wx.hideLoading();
        }
      }
    });
  })
}